﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Themes
{
    /// <summary>
    /// 主题管理器
    /// </summary>
    public class ThemeManager
    {
        /// <summary>
        /// 应用指定主题资源字典。
        /// </summary>
        /// <param name="themeName">主题名称"LightTheme"/"DarkTheme"</param>
        public static void ApplyTheme(string themeName)
        {
            if (string.IsNullOrWhiteSpace(themeName))
                return;

            //var themeUri = new Uri($"Themes/{themeName}.xaml", UriKind.Relative);
            var themeUri = new Uri($"pack://application:,,,/Themes;component/{themeName}.xaml", UriKind.Absolute);
            

            // 加载新主题资源字典
            var newTheme = new ResourceDictionary { Source = themeUri };

            // 清除旧的主题资源（只移除 Themes 文件夹下的）
            var existingThemes = Application.Current.Resources.MergedDictionaries
                .Where(d => d.Source != null && d.Source.OriginalString.StartsWith("Themes/"))
                .ToList();

            foreach (var oldTheme in existingThemes)
            {
                Application.Current.Resources.MergedDictionaries.Remove(oldTheme);
            }

            // 添加新主题
            Application.Current.Resources.MergedDictionaries.Add(newTheme);
        }
    }
}
